package org.act.xservices.rt.xcontainer.framework.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.act.xservices.rt.metaset.Metaset;
import org.act.xservices.rt.metaset.MetasetBuilder;
import org.act.xservices.rt.metaset.MetasetBuilderFactory;
import org.act.xservices.rt.metaset.exception.MetasetParsingException;
import org.w3c.dom.Element;

public class XContainerUtil {
	
	private static final MetasetBuilder metBuilder = MetasetBuilderFactory.newInstance().getMetasetBuilder();
	
	@SuppressWarnings("unused")
	public static Metaset ChangeDomElementToMetaset(Element domElement){
		Metaset met = null;
		if(domElement == null){
			return null;
		}
		
        try {
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
			transformer.transform(new DOMSource(domElement), new StreamResult(byteOutStream));
			met = metBuilder.parse(new ByteArrayInputStream(byteOutStream.toByteArray()));
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		} catch (MetasetParsingException e) {
			e.printStackTrace();
		}
		return met;
	}

}
